package io.renren.modules.app.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.entity.MemberPurseEntity;
import io.renren.modules.app.entity.vo.QueryOrderParam;
import io.renren.modules.app.entity.vo.SelectOrderData;
import io.renren.modules.app.service.MemberPurseService;
import io.renren.modules.app.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.app.entity.OrdersEntity;
import io.renren.modules.app.service.OrdersService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 订单表
 *
 * @date 2022-04-20 22:37:44
 */
@RestController
@RequestMapping("app/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private MemberPurseService memberPurseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:orders:list")
    public R list(QueryOrderParam order) {
        SelectOrderData selectOrderData = ordersService.queryPage(order);
        return R.ok().put("page", selectOrderData);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{orderId}")
    @RequiresPermissions("generator:orders:info")
    public R info(@PathVariable("orderId") Integer orderId) {
        OrdersEntity orders = ordersService.getById(orderId);

        return R.ok().put("orders", orders);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:orders:save")
    public R save(@RequestBody OrdersEntity orders) {
        orders.setCreateTime(new Date());
        ordersService.save(orders);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:orders:update")
    public R update(@RequestBody OrdersEntity orders) {
        ordersService.updateById(orders);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:orders:delete")
    public R delete(@RequestBody Integer[] orderIds) {
        ordersService.removeByIds(Arrays.asList(orderIds));

        return R.ok();
    }

    /**
     * 新增订单
     *
     * @param order 订单信息类
     * @return
     */
    @Login
    @PostMapping("addOrder")
    public R addOrder(@RequestBody OrdersEntity order) {
        Integer integer = ordersService.addOrder(order);
        if (integer > 0) {
            return R.ok("新增成功");
        } else {
            return R.error("新增失败");
        }
    }

    /**
     * 分页获取订单数据
     *
     * @param order 订单信息类
     * @return 订单数据
     */
    @Login
    @GetMapping("queryOrderList")
    public R query(QueryOrderParam order) {
        if (StringUtils.isNull(order.getUserId())) {
            return R.error("用户id不能为空");
        }
        if (StringUtils.isNull(order.getCount())) {
            return R.error("筛选数据参数不能为空");
        }
        SelectOrderData selectOrderData = ordersService.queryOrderList(order);
        return R.ok().put("data", selectOrderData);
    }

    /**
     * 获取订单详情
     *
     * @param ordersEntity 订单信息类
     * @return 订单详情
     */
    @Login
    @GetMapping("queryOrderDetails")
    public R queryOrderDetails(OrdersEntity ordersEntity) {
        OrdersEntity orders = ordersService.queryOrderDetails(ordersEntity);
        return R.ok().put("data", orders);
    }


    /**
     * 支付
     *
     * @param order 订单信息类
     * @return 结果
     */
    @PostMapping("payOrder")
    public R payOrder(@RequestBody OrdersEntity order) {
        //判断余额是否充值
        MemberPurseEntity memberPurse = memberPurseService.queryUserPurse(order.getUserId());
        if (memberPurse.getWithdrawalsAmount().compareTo(order.getTransactionAmount()) == -1) {
            return R.error("余额不足,请充值再支付");
        }

        MemberPurseEntity memberPurseE = new MemberPurseEntity();
        memberPurseE.setUserId(order.getUserId());
        //钱包余额 = 当前余额 - 订单金额
        memberPurseE.setWithdrawalsAmount(memberPurse.getWithdrawalsAmount().subtract(order.getTransactionAmount()));
        //加上待返金额
        memberPurseService.editMemberPurse(memberPurseE);

        order.setOrderStatus(1);
        //修改订单状态
        ordersService.editOrderId(order);
        return R.ok();
    }

}
